// pages/login/register/register.js
var area = require('../../../utils/area.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: true,
    actions: [{
      name: '授权',
      color: '#07c160',
      openType: 'getUserInfo'
    }, ],

    avatarUrl: "",
    openid: "",

    phone: "",
    password: "",
    confirmPassword: "",
    name: "",
    idCard: "",
    sex: null,
    address: "",
    houseInfo: "",

    phoneError: "",
    passwordError: "",
    confirmPasswordError: "",
    nameError: "",
    idCardError: "",
    houseInfoError: "",

    areaShow: false,
    selectAreaCode: "441900",
    areaList: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      areaList: area.default
    });
  },
  submitInfo() {
    //检查注册信息是否有误
    if (this.data.phoneError != "" || this.data.passwordError != "" ||
      this.data.confirmPasswordError != "" || this.data.nameError != "" ||
      this.data.idCardError != "" || this.data.houseInfoError != "") {
      wx.showToast({
        title: '注册信息有误，请检查一下',
        icon: 'none',
        duration: 1500
      });
      return;
    }
    //检查注册信息是否为空
    if (this.data.phone == "" || this.data.password == "" ||
      this.data.name == "" || this.data.idCard == "" || this.data.sex == null ||
      this.data.address == "" || this.data.houseInfo == "") {
      wx.showToast({
        title: '注册信息不能为空，请检查一下',
        icon: 'none',
        duration: 1500
      });
      return;
    }
    //检查授权
    if (this.data.avatarUrl == "" || this.data.openid == "") {
      let that = this;
      wx.showToast({
        title: '请授权获得你的公开信息(昵称，头像等)',
        icon: 'none',
        duration: 1500,
        complete() {
          that.setData({
            show: true
          })
        }
      });
      return;
    }


    //提交注册信息
    wx.request({
        url: getApp().globalData.BASE_URL + "/anonymous/user/register",
        method: "POST",
        data: {
          avatarUrl: this.data.avatarUrl,
          openid: this.data.openid,
          name: this.data.name,
          phone: this.data.phone,
          password: this.data.password,
          idCard: this.data.idCard,
          sex: this.data.sex,
          address: this.data.address,
          houseInfo: this.data.houseInfo
        },
        success(res) {
          if(res.data.code == 200){
            wx.showToast({
              title: '注册成功！请前往登录',
              duration: 1500,
              complete() {
                wx.navigateTo({
                  url: '../index/index',
                })
              }
            });
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 1500
            });
          }
        }
    })
},
onClose() {
  this.setData({
    show: false
  });
},

onGetUserInfo(e) {
  // console.log(e.detail);
  this.setData({
    avatarUrl: e.detail.userInfo.avatarUrl
  })

  //获取openId
  let _this = this;
  wx.login({
    success: res => {
      // 发送 res.code 到后台换取 openId, sessionKey, unionId
      wx.request({
        url: getApp().globalData.BASE_URL + "/anonymous/wx/openid/user",
        method: "GET",
        data: {
          code: res.code
        },
        success(res) {
          // console.log(res.data)
          res = getApp().responseInterceptor(res)
          if (res.isRegister){
            //该微信号已经注册过
            wx.showModal({
              title: '提示',
              content: '你这个微信号已经注册过账号了，可以尝试一键登录',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                 wx.navigateTo({
                   url: '../index/index',
                 })
                }
              }
            })
          }
          let obj = JSON.parse(res.msg);
          _this.setData({
            openid: obj.openid
          })
        }
      })
    }
  })
},
checkPhone(e) {
  let value = e.detail.value;
  let message = "";
  var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0-9]{1})|(15[0-3]{1})|(15[4-9]{1})|(18[0-9]{1})|(199))+\d{8})$/;
  if (value == '') {
    message = "手机号码不能为空！";
  } else if (value.length != 11) {
    message = "请输入11位手机号码！";
  } else if (!myreg.test(value)) {
    message = "请输入有效的手机号码！";
  } else {
    let _this = this;
    //查询后台该手机号是否被注册
    wx.request({
      url: getApp().globalData.BASE_URL + "/anonymous/user/phone/judge",
      method: "GET",
      data: {
        phone: value
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.code === 200 && res.data.data === true) {
          message = "该号码已经被注册";
          _this.setData({
            phoneError: message
          })
        }
      }
    })
  }
  this.setData({
    phoneError: message
  })
},
checkPassword(e) {
  let value = e.detail.value;
  if (value == "") {
    this.setData({
      passwordError: "密码不能为空"
    })
  } else {
    this.setData({
      passwordError: ""
    })
  }
},
checkConfirmPassword(e) {
  let value = e.detail.value;
  if (value != this.data.password) {
    this.setData({
      confirmPasswordError: "两次密码输入不一致！"
    })
  } else {
    this.setData({
      confirmPasswordError: ""
    })
  }
},
checkName(e) {
  let value = e.detail.value;
  if (value == "") {
    this.setData({
      nameError: "姓名不能为空"
    })
  } else {
    this.setData({
      nameError: ""
    })
  }
},
checkIdCard(e) {
  let value = e.detail.value;
  if (value == "") {
    this.setData({
      idCardError: "身份证号不能为空"
    })
  } else if (value.length != 18) {
    this.setData({
      idCardError: "请输入18位身份证号"
    })
  } else {
    this.setData({
      idCardError: ""
    })
  }
},
checkHouseInfo(e) {
  let value = e.detail.value;
  if (value == "") {
    this.setData({
      houseInfoError: "详细地址不能为空"
    })
  } else {
    this.setData({
      houseInfoError: ""
    })
  }
},
changePhone(event) {
  this.setData({
    phone: event.detail,
  });
},
changePassword(event) {
  this.setData({
    password: event.detail,
  });
},
changeName(event) {
  this.setData({
    name: event.detail,
  });
},
changeIdCard(event) {
  this.setData({
    idCard: event.detail,
  });
},
onChangeSex(event) {
  this.setData({
    sex: event.detail,
  });
},
onChangeDetailedAddress(event) {
  this.setData({
    houseInfo: event.detail
  })
},
onClickAreaIcon() {
  this.setData({
    areaShow: true
  });
},
onCancelArea() {
  this.setData({
    areaShow: false
  });
},
onSelectArea(event) {
  console.log(event.detail.values[2].code)
  let selectArea = event.detail.values[0].name + event.detail.values[1].name + event.detail.values[2].name;
  this.setData({
    areaShow: false,
    selectAreaCode: event.detail.values[2].code.code,
    address: selectArea
  });
},

/**
 * 生命周期函数--监听页面初次渲染完成
 */
onReady: function() {

},

/**
 * 生命周期函数--监听页面显示
 */
onShow: function() {

},

/**
 * 生命周期函数--监听页面隐藏
 */
onHide: function() {

},

/**
 * 生命周期函数--监听页面卸载
 */
onUnload: function() {

},

/**
 * 页面相关事件处理函数--监听用户下拉动作
 */
onPullDownRefresh: function() {

},

/**
 * 页面上拉触底事件的处理函数
 */
onReachBottom: function() {

},

/**
 * 用户点击右上角分享
 */
onShareAppMessage: function() {

}
})